package com.suse.blog.config;

import com.suse.blog.enums.ResultCodeEnum;
import com.suse.blog.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.resource.NoResourceFoundException;

@Slf4j
@RestControllerAdvice
public class ExceptionAdvice {

    @ExceptionHandler
    public Result handle(Exception e) {
        log.error("发生异常, e:{}",e.getMessage());
        return Result.fail(ResultCodeEnum.PARAM_INVALID.getCode(),e.getMessage());
    }

    @ResponseStatus(code = HttpStatus.NOT_FOUND)
    @ExceptionHandler
    public Result handle(NoResourceFoundException e) {
        log.error("文件不存在, e:{}",e.getResourcePath());
        return Result.fail(ResultCodeEnum.NO_RESOUSES.getCode(),e.getResourcePath());
    }

    @ExceptionHandler
    public Result handle(IllegalArgumentException e) {
        log.error("发生异常, e:{}",e.getMessage());
        return Result.fail(ResultCodeEnum.PARAM_INVALID.getCode(),e.getMessage());
    }

    @ExceptionHandler
    public Result handle(MethodArgumentNotValidException e) {
        log.error("参数校验不通过, e:{}",e.getMessage());
        return Result.fail(ResultCodeEnum.PARAM_INVALID.getCode(),e.getMessage());
    }
}
